package com.nomito.minimito.service.impl;

import com.fasterxml.jackson.databind.util.BeanUtil;
import com.nomito.minimito.dao.SqTopicCommentDao;
import com.nomito.minimito.entity.SqActivityCommentEntity;
import com.nomito.minimito.entity.SqTopicCommentEntity;
import com.nomito.minimito.service.SqTopicCommentService;
import com.nomito.minimito.vo.TopicVo;
import com.nomito.minimito.vo.UserTopicVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nomito.minimito.common.utils.PageUtils;
import com.nomito.minimito.common.utils.Query;

import com.nomito.minimito.dao.SqTopicDao;
import com.nomito.minimito.entity.SqTopicEntity;
import com.nomito.minimito.service.SqTopicService;


@Service("sqTopicService")
public class SqTopicServiceImpl extends ServiceImpl<SqTopicDao, SqTopicEntity> implements SqTopicService {

    @Autowired
    SqTopicCommentService sqTopicCommentService;
    @Autowired
    SqTopicCommentDao sqTopicCommentDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SqTopicEntity> page = this.page(
                new Query<SqTopicEntity>().getPage(params),
                new QueryWrapper<SqTopicEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<SqTopicEntity> getTopicList(Integer key) {

       List<SqTopicEntity> sqTopicEntities = new ArrayList<>();
        if (key == 0){//最早
            sqTopicEntities=this.baseMapper.selectList(new QueryWrapper<SqTopicEntity>().eq("is_deleted",0).orderByAsc("gmt_create"));
        }else if (key==1){//最晚
           sqTopicEntities= this.baseMapper.selectList(new QueryWrapper<SqTopicEntity>().eq("is_deleted",0).orderByDesc("gmt_create"));
        }else {//最热
            sqTopicEntities= this.baseMapper.selectList(new QueryWrapper<SqTopicEntity>().eq("is_deleted",0).orderByDesc("star_count"));

        }
        return sqTopicEntities;
    }

    @Override
    public void insertTopicDetail(TopicVo topicVo) {

        SqTopicEntity sqTopicEntity = new SqTopicEntity();
        BeanUtils.copyProperties(topicVo,sqTopicEntity);
        sqTopicEntity.setStarCount(0);
        this.baseMapper.insert(sqTopicEntity);

    }

    @Override
    public UserTopicVo getUserTopicList(Long userId) {
        UserTopicVo userTopicVo = new UserTopicVo();
        List<SqTopicEntity> sqTopicEntities = this.list(new QueryWrapper<SqTopicEntity>().eq("send_id",userId).ne("is_deleted",1));
        userTopicVo.setSqTopicEntityList(sqTopicEntities);

        List<SqTopicCommentEntity> sqTopicCommentEntities = sqTopicCommentService.list(new QueryWrapper<SqTopicCommentEntity>().eq("comment_id",userId).ne("is_deleted",1));
        List<SqTopicEntity> sqTopicEntities1 = sqTopicCommentEntities.stream().map(item ->{
            SqTopicEntity sqTopicEntity = new SqTopicEntity();
            sqTopicEntity = this.baseMapper.selectById(item.getTopicId());
            return sqTopicEntity;
        }).collect(Collectors.toList());
        userTopicVo.setUserTopicCommentEntityList(sqTopicEntities1);

        return userTopicVo;
    }

}